/**
 * 
 */
package cloud.thrift.conf;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.PostConstruct;

import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.transport.TTransportException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import cloud.thrift.service.UserService;
import cloud.thrift.service.UserServiceImpl;

/**
 * @author zhaodarong
 *
 */
@Configuration
public class ThriftConfig {
	ExecutorService es = Executors.newSingleThreadExecutor();
	
	@Bean
	public TServerTransport tServerTransport() {
		try {
			return new TServerSocket(7191);
		} catch (TTransportException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	@Bean
	public TServer tServer() {
		// deploy service
		UserService.Processor processor = new UserService.Processor(new UserServiceImpl());
		TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(tServerTransport()).processor(processor));
		
		return server;
	}
	
	@PostConstruct
	public void init() {
		es.execute(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				tServer().serve();
			}
			
		});
	}

}
